<?php
class InvoicesController extends AppController {

	var $name = 'Invoices';
	var $uses= array('Invoice','InvoicesDetail','Member');
	function index() {
		$this->Invoice->recursive = 0;		
		$this->paginate = array('conditions'=>array('Invoice.type'=>0),'order'=>'Invoice.id  DESC','limit' =>20);
        $invoices = $this->paginate('Invoice');
		$this->set('invoices',$invoices);
	}
	
	function actives($id=null) {
		$invoice=array('Invoice'=>array(
									'id'=>$id,
									'status'=>1
										));
		if($this->Invoice->save($invoice)){								
		$name=$_SERVER['HTTP_REFERER'];
		$this->redirect($name);
		}
	}
	
	function closes($id=null) {
		$invoice=array('Invoice'=>array(
								'id'=>$id,
								'status'=>0
								));
		if($this->Invoice->save($invoice)){								
		$name=$_SERVER['HTTP_REFERER'];
		$this->redirect($name);
		}

	}
	
	function add() {

	}
	
   function view($id=null) {
	 if(!$id){
		$name=$_SERVER['HTTP_REFERER'];
		$this->redirect($name);
	}else{
		$idMember=$this->Invoice->findById($id);
		$this->set('members',$this->Member->findById($idMember['Invoice']['id_user']));
		$this->set('views',$this->InvoicesDetail->find('all',array('conditions'=>array('InvoicesDetail.id_invoice'=>$id),'order'=>'InvoicesDetail.id DESC')));
	}
	   

	}
	
	function delete() {
		if($_POST['chkId']){
			$Idnews=$_POST['chkId'];
			foreach($Idnews as $id){
				$this->Invoice->delete($id);			
			}
			$this->Session->setFlash(__('Bạn Delete thành công', true));
			$name=$_SERVER['HTTP_REFERER'];
			$this->redirect($name);
		}

	}




}
?>